package chapter3;

class ColorAttr extends Attr {
    private ScreenColor myColor;
    private String red = "red";
    
    public ColorAttr(String name, Object value){
        super(name, value);
        decodeColor();
    }
    
    public ColorAttr(String name){
        //compile error, Cannot refer to an instance field red while explicitly invoking a constructor
        //as at this phase, red isn't initialized to "red"
        //super(red);
        this(name, "transparent");
    }
    
    public ColorAttr(String name, ScreenColor value){
        super(name, value.toString());
        myColor = value;
    }
    
    public Object setValue(Object newValue){
        //override, do the supperclass's setValue work first
        Object retval = super.setValue(newValue);
        decodeColor();
        return retval;
    }
    
    public ScreenColor setValue(ScreenColor newValue){
        //overload, to the supperclass's setValue work first
        super.setValue(newValue.toString());
        ScreenColor oldValue = myColor;
        myColor = newValue;
        return oldValue;
    }
    
    public ScreenColor getColor(){
        return myColor;
    }
    
    protected void decodeColor(){
        if(getValue() == null){
            myColor = null;
        }else{
            myColor = new ScreenColor(getValue());
        }
    }
    
    public boolean equals(Object obj){
        if(obj instanceof ColorAttr){
            return myColor.equals(obj);
        }else{
            return false;
        }
    }
    
    public int hashCode(){
        if(myColor == null){ 
            return super.hashCode();
        }else{
            return myColor.hashCode();
        }
    }

}
